﻿using LibraryManagement.Custom_Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LibraryManagement.Custom_Controls.Data_Grid_View;
using LibraryManagement.Inventory;

namespace LibraryManagement.Search
{
    public partial class SrcBookSearch : libForm
    {
        private Redirection redirection;
        private LibraryManagement.ParentForm parentForm;

        public enum Redirection { Search = 0, CopiesAdd = 1, Summary = 2 }

        public SrcBookSearch()
        {
            InitializeComponent();
        }

        public SrcBookSearch(Redirection redirection, LibraryManagement.ParentForm parentForm)
        {
            InitializeComponent();
            this.redirection = redirection;
            this.parentForm = parentForm;
            PrepareGrid();
        }

        private void PrepareGrid()
        {
            if (redirection == Redirection.Search)
            {

            }
            else if (redirection == Redirection.CopiesAdd)
            {
                dgvBooks.Columns.Add(new libDataGridViewColumnSelect());
                SetDataGridView();
            }

        }

        public void SetDataGridView()
        {
            var context = new LibManagementEntities();
            dgvBooks.DataSource = context.BookDetails.Where(x => x.UserId == clsStatics.currentUserId).ToList();
        }
        public void SetDataGridView(object dataSource)
        {
            dgvBooks.DataSource = dataSource;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvBooks_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex == dgvBooks.Columns["dgvcBtnSelect"].Index)
            {
                var bookId = (decimal)dgvBooks.Rows[e.RowIndex].Cells["id"].Value;
                InvBookCopiesAdd OInvBookCopiesAdd = new InvBookCopiesAdd(bookId, this);
                OInvBookCopiesAdd.MdiParent = parentForm;
                OInvBookCopiesAdd.Show();
                this.Enabled = false;
            }
        }

        private void txtTitle_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void ApplyFilter()
        {
            var context = new LibManagementEntities();
            var listFilteredBooks = context.BookDetails.Where(x => x.UserId == clsStatics.currentUserId).ToList();
            if (txtTitle.Text.Trim() != "")
            {
                listFilteredBooks = listFilteredBooks.Where(x => x.Title.ToLower().Contains(txtTitle.Text.ToLower())).ToList();
            }
            if (txtBookId.Text != "")
            {
                var bookId = Convert.ToDecimal(txtBookId.Text);
                listFilteredBooks = listFilteredBooks.Where(x => x.Id == bookId).ToList();
            }
            if (txtLibCode.Text != "")
            {
                var libCode = Convert.ToDecimal(txtLibCode.Text);
                decimal? bookId;
                try
                {
                    bookId = context.BookCopies.Single(x => x.LibCode == libCode).BookId;
                    listFilteredBooks = listFilteredBooks.Where(x => x.Id == bookId).ToList();
                }
                catch (Exception)
                {
                }

            }
            if (txtAuthor.Text.Trim() != "")
            {
                var author = txtAuthor.Text.ToLower();
                listFilteredBooks = listFilteredBooks.Where(x => x.Author1.ToLower().Contains(author)).ToList();
            }
            if (txtPublisher.Text.Trim() != "")
            {
                listFilteredBooks = listFilteredBooks.Where(x => x.Publisher.ToLower().Contains(txtPublisher.Text.ToLower())).ToList();
            }
            if (txtISBN.Text != "")
            {
                try
                {
                    var ISBN = Convert.ToDecimal(txtISBN.Text);
                    listFilteredBooks = listFilteredBooks.Where(x => x.ISBN == ISBN).ToList();
                }
                catch (Exception)
                {
                }
            }
            dgvBooks.DataSource = listFilteredBooks;
        }

        private void txtLibCode_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void txtBookId_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void txtAuthor_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void txtPublisher_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }

        private void txtISBN_TextChanged(object sender, EventArgs e)
        {
            ApplyFilter();
        }
    }
}
